package com.wjc.train.member.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wjc.train.common.context.LoginMemberContext;
import com.wjc.train.common.enums.LogicEnum;
import com.wjc.train.common.untils.CopyUtil;
import com.wjc.train.member.entity.Passenger;
import com.wjc.train.member.repository.PassengerRepository;
import com.wjc.train.member.req.PassengerQueryReq;
import com.wjc.train.member.req.PassengerSaveReq;
import com.wjc.train.member.resp.PassengerQueryResp;
import com.wjc.train.member.service.IPassengerService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 乘车人表 服务实现类
 * </p>
 *
 * @author Guessed
 * @since 2023-07-26
 */
@Service
public class PassengerServiceImpl implements IPassengerService {

    @Resource
    private PassengerRepository passengerRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(PassengerSaveReq passengerSaveReq) {
        Passenger passenger = CopyUtil.copy(passengerSaveReq, Passenger.class);
        passenger.setMemberId(LoginMemberContext.getId());
        passengerRepository.saveOrUpdate(passenger);
    }

    @Override
    public IPage<PassengerQueryResp> queryList(IPage<PassengerQueryResp> page, PassengerQueryReq passengerQueryReq) {
        passengerQueryReq.setMemberId(LoginMemberContext.getId());
        return passengerRepository.queryList(page,passengerQueryReq);
    }

    @Override
    public void delete(Long id) {
        passengerRepository.getBaseMapper().deleteById(id);
    }

    @Override
    public List<PassengerQueryResp> queryMine() {
        List<Passenger> list = passengerRepository.lambdaQuery()
                        .eq(Passenger::getIsDeleted, LogicEnum.NOT_DELETED.getCode())
                        .eq(Passenger::getMemberId, LoginMemberContext.getId())
                        .list();
        return  CopyUtil.copyList(list, PassengerQueryResp.class);
    }
}
